cmake_minimum_required(VERSION 3.10)
project(sHamLogX11)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find required packages
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
pkg_check_modules(GLIB REQUIRED glib-2.0)
pkg_check_modules(WEBKIT2 REQUIRED webkit2gtk-4.1)

# Find SQLite3
find_package(PkgConfig REQUIRED)
pkg_check_modules(SQLITE3 REQUIRED sqlite3)

# Include directories
include_directories(${GTK3_INCLUDE_DIRS} ${GLIB_INCLUDE_DIRS} ${WEBKIT2_INCLUDE_DIRS} ${SQLITE3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS} ${GLIB_LIBRARY_DIRS} ${WEBKIT2_LIBRARY_DIRS} ${SQLITE3_LIBRARY_DIRS})
add_definitions(${GTK3_CFLAGS_OTHER} ${GLIB_CFLAGS_OTHER} ${WEBKIT2_CFLAGS_OTHER} ${SQLITE3_CFLAGS_OTHER})

# Add source files
set(SOURCES
    src/main.cpp
    src/config/config.cpp
    src/config/cabrillo_config.cpp
    src/config/window_config.cpp
    src/app/main_app.cpp
    src/ui/ui_manager.cpp
    src/ui/function_button_manager.cpp
    src/ui/cabrillo_config_dialog.cpp
    src/ui/log_grid_dialog.cpp
    src/ui/qso_edit_dialog.cpp
    src/ui/help_window.cpp
    src/ui/map_window.cpp
    src/serial/serial_manager.cpp
    src/radio/radio_manager.cpp
    src/http/http_server.cpp
    src/database/sqlite_manager.cpp
    src/database/cty_dat_parser.cpp
)

# Create executable
add_executable(${PROJECT_NAME} ${SOURCES})

# Link libraries
target_link_libraries(${PROJECT_NAME} ${GTK3_LIBRARIES} ${GLIB_LIBRARIES} ${WEBKIT2_LIBRARIES} ${SQLITE3_LIBRARIES})

# Create CTY test executable
add_executable(cty_test_app src/app/cty_test_app.cpp src/database/cty_dat_parser.cpp)
target_include_directories(cty_test_app PRIVATE src)

# Copy UI files to build directory
# 使用configure_file确保UI文件在每次构建时都会更新
file(GLOB UI_FILES "UI/*")
foreach(UI_FILE ${UI_FILES})
    get_filename_component(FILE_NAME ${UI_FILE} NAME)
    configure_file(${UI_FILE} ${CMAKE_BINARY_DIR}/UI/${FILE_NAME} COPYONLY)
endforeach()

# Copy config files to build directory
file(COPY config DESTINATION ${CMAKE_BINARY_DIR})

# Install UI files
install(DIRECTORY UI DESTINATION ${CMAKE_INSTALL_PREFIX}/share/sHamLogX11)

# Install config files
install(DIRECTORY config DESTINATION ${CMAKE_INSTALL_PREFIX}/share/sHamLogX11)

# 增加 -g 和 -rdynamic 选项
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -rdynamic")

# 复制 工作空间的cwtrainerX 到 build 目录
file(COPY ./cwtrainerX DESTINATION ${CMAKE_BINARY_DIR})

# 复制工作目录下的libjqueryreplace.so文件到build/extensions/extension/目录
file(COPY ./libjqueryreplace.so DESTINATION ${CMAKE_BINARY_DIR}/extensions/extension/)